import axios from 'axios';

// 创建一个简单的axios实例
const apiClient = axios.create({
    baseURL: '/feign',  // 使用 Vite 代理，不需要端口号
    timeout: 10000,
    headers: {
        'Content-Type': 'application/json'
    }
});

// 添加请求拦截器
apiClient.interceptors.request.use(
  config => {
    console.log('发送请求:', config);
    return config;
  },
  error => {
    console.error('请求拦截器错误:', error);
    return Promise.reject(error);
  }
);

// 添加响应拦截器
apiClient.interceptors.response.use(
  response => {
    console.log('响应成功:', response);
    // 直接返回整个响应对象，由调用方处理
    return response;
  },
  error => {
    console.error('响应错误:', error);
    if (error.response) {
      console.error('响应数据:', error.response.data);
      // 返回一个统一的错误格式
      return Promise.reject({
        status: error.response.status,
        message: error.response.data?.message || error.message,
        data: error.response.data
      });
    }
    return Promise.reject(error);
  }
);

// 基础请求方法
const request = {
  // GET 请求
  async get(url, params = {}) {
    try {
      console.log(`发送GET请求: ${url}`, { params });
      const response = await apiClient.get(url, { params });
      console.log(`GET请求响应: ${url}`, response);
      return response;
    } catch (error) {
      console.error('GET请求失败:', {
        url,
        error: error.message,
        response: error.response?.data || '无响应数据',
        status: error.response?.status,
        statusText: error.response?.statusText,
        headers: error.response?.headers
      });
      throw error;
    }
  },
  
  // POST 请求
  async post(url, data = {}) {
    try {
      console.log(`发送POST请求: ${url}`, { data });
      const response = await apiClient.post(url, data);
      console.log(`POST请求响应: ${url}`, response);
      return response;
    } catch (error) {
      console.error('POST请求失败:', {
        url,
        error: error.message,
        response: error.response?.data || '无响应数据'
      });
      throw error;
    }
  },
  
  // PUT 请求
  async put(url, data = {}) {
    try {
      console.log(`发送PUT请求: ${url}`, { data });
      const response = await apiClient.put(url, data);
      console.log(`PUT请求响应: ${url}`, response);
      return response;
    } catch (error) {
      console.error('PUT请求失败:', {
        url,
        error: error.message,
        response: error.response?.data || '无响应数据'
      });
      throw error;
    }
  },
  
  // DELETE 请求
  async del(url) {
    try {
      console.log(`发送DELETE请求: ${url}`);
      const response = await apiClient.delete(url);
      console.log(`DELETE请求响应: ${url}`, response);
      return response;
    } catch (error) {
      console.error('DELETE请求失败:', {
        url,
        error: error.message,
        response: error.response?.data || '无响应数据'
      });
      throw error;
    }
  }
};

// 文化遗产相关API
export const listHeritage = (id) => 
  id ? request.get(`/heritage/${id}`) : request.get('/heritage');

export const searchHeritage = (keyword) => 
  request.get(`/heritage/search?keyword=${encodeURIComponent(keyword)}`);

export const createHeritage = (data) => 
  request.post('/heritage', data);

export const updateHeritage = (id, data) => 
  request.put(`/heritage/${id}`, data);

export const deleteHeritage = (id) => 
  request.del(`/heritage/${id}`);

// 导出 apiClient 供其他需要直接使用 axios 实例的地方使用
export { apiClient };

export default {
  listHeritage,
  searchHeritage,
  createHeritage,
  updateHeritage,
  deleteHeritage,
  request,
  apiClient
};